package edu.mit.media.fv.layers
{
import edu.mit.media.fv.*;
import edu.mit.media.fv.blocks.*;
import edu.mit.media.fv.components.*;
import edu.mit.media.fv.events.BlockEvent;
import edu.mit.media.fv.fluidspace.*;
import edu.mit.media.fv.icons.AQHandleIcon;

import flash.display.*;
import flash.events.*;
import flash.geom.Rectangle;
	
	public class Handle extends Sprite
	{
		
		private static var wb:int = 20;
		private static var arc:int = 10;
		private static var inset:int = 5;
		private static var handleColor:uint = 0x999999;//0x2C2416;
		private static var arrowColor:uint = 0xCCCCCC;
		
		private var xLoc:int = 0;
		private var yLoc:int = 0;
		private var dragBounds:Rectangle;
		private var dragging:Boolean = false;
		private var bounds:Object;
		private var bar:DisplayObject;
		private var layer:Layer;
		
		public function Handle(layer:Layer){
			super();
			this.layer = layer;
			bounds = new Object();
			this.dragBounds = new Rectangle(0,-1000,0,2000);
						
			this.bar = new AQHandleIcon();
			this.addChild(this.bar);
			this.bar.addEventListener(MouseEvent.MOUSE_DOWN, processMouseDown);
			this.addEventListener(MouseEvent.MOUSE_UP, processMouseUp);
			//this.addEventListener(MouseEvent.ROLL_OUT, onMouseUp);
		}
		private function processMouseDown(event:MouseEvent):void{
			//this.startDrag(false, this.dragBounds);
			this.startDrag(false);
			this.dragging = true;
			this.draw();
			var blocks:Array =  this.blocks;
			for(var i:int = 0; i<blocks.length; i++){
				blocks[i].logDragPoint();
			}
			var e:BlockEvent = new BlockEvent(BlockEvent.HANDLE_DRAGGED, null, null);
			e.handle = this;
			FluidEventHandler.dispatchEvent(e);
		}
		private function processMouseUp(event:MouseEvent):void{
			this.stopDrag();			
			this.dragging = false;
			this.draw();
			var e:BlockEvent = new BlockEvent(BlockEvent.HANDLE_DROPPED, null, null);
			e.handle = this;
			FluidEventHandler.dispatchEvent(e);
			
		}
		public function get blocks():Array{
			return this.layer.blocks;
		}
		public function setBounds(x:int, y:int, w:int, h:int):void {
			this.x = x;
			this.y = y;
			this.xLoc = x;
			this.yLoc = y;
			bounds.h = h;
			bounds.w = w;
			bounds.height = h-inset*2;
			bounds.width = wb;
			
			bounds.y1 = inset;
			bounds.y2 = inset+arc;
			bounds.y3 = inset+arc+wb/4;
			bounds.y4 = h-inset-arc-wb/4;
			bounds.y5 = h-inset-arc;
			bounds.y6 = h-inset;
			
			bounds.x1 = w-inset-wb;
			bounds.x2 = w-inset-wb+arc;
			bounds.x3 = w-inset-wb*3/4;
			bounds.x4 = w-inset-wb/2;
			bounds.x5 = w-inset-wb/4;
			bounds.x6 = w-inset-arc;
			bounds.x7 = w-inset;
			
			this.draw();
		}
		private function draw():void{
			this.graphics.clear();
			
			if(this.dragging){
				this.graphics.beginFill(0xFFFFFF, 0.25);
				this.graphics.lineStyle(1, 0xFFFFFF, .5);
				this.graphics.drawRoundRect(0, 0, bounds.w, bounds.h, 15, 15);
				this.graphics.endFill();
			}
			
			this.bar.x = 10;
			this.bar.y = 10;
		}
		public function reset():void{
			this.layer.addChild(this);
			this.x = this.xLoc;
			this.y = this.yLoc;
		}
	}
}